package com.xizimall.portal.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.xizimall.pojo.TbItem;
import com.xizimall.pojo.TbUser;
import com.xizimall.portal.pojo.CartItem;
import com.xizimall.portal.service.CartService;
import com.xizimall.common.pojo.XizimallResult;
import com.xizimall.common.utils.CookieUtils;
import com.xizimall.common.utils.HttpClientUtil;
import com.xizimall.common.utils.JsonUtils;
/**
 * 购物车服务
 * <p>Title: CartServiceImpl</p>
 * <p>Description: </p>
 * <p>华南理工大学研究生</p> 
 * @author	李隆熹
 * @date	2018年4月27日下午7:09:39
 * @version 1.0
 */
@Service
public class CartServiceImpl implements CartService {
    
	@Value("${REST_BASE_URL}")
	private String REST_BASE_URL;
	
	@Value("${ITME_INFO_URL}")
	private String ITME_INFO_URL;
	/**
     * 添加购物车
     * <p>Title: addCartItem</p>
     * <p>Description: </p>
     * @param itemId
     * @param num
     * @return
     */
	  
	@Override
	public XizimallResult addCartItem(Long itemId, Integer num,
			HttpServletRequest request, HttpServletResponse response) {
		CartItem cartItem = null;
		List<CartItem> itemList = getCartItemList(request) ;
		//判断商品列表中是否存在此商品
		for(CartItem citem:itemList){
			if(citem.getId()==itemId){
				citem.setNum(citem.getNum()+num);
				cartItem=citem;
				break;
			}
		}
		if(cartItem==null){
			cartItem = new CartItem();
			String json = HttpClientUtil.doGet(REST_BASE_URL+ITME_INFO_URL+itemId);
			XizimallResult result = XizimallResult.formatToPojo(json, TbItem.class);
			if(result.getStatus()==200){
				TbItem item = (TbItem) result.getData();
				cartItem.setId(item.getId());
				cartItem.setTitle(item.getTitle());
				cartItem.setImage(item.getImage() == null ? "":item.getImage().split(",")[0]);
				cartItem.setNum(num);
				cartItem.setPrice(item.getPrice());
			}
			itemList.add(cartItem);
		}
		//把购物车列表写入Cookies
		CookieUtils .setCookie(request, response, "TT_CART",JsonUtils.objectToJson(itemList) , true);
		return XizimallResult.ok();
	}
	private List<CartItem> getCartItemList(HttpServletRequest request){
		//从cookie中取商品列表
		String cartJson = CookieUtils.getCookieValue(request, "TT_CART", true);
		if(cartJson==null){
			return new ArrayList<>();
		}
		try {
			List<CartItem> list = JsonUtils.jsonToList(cartJson, CartItem.class);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
	  return new ArrayList<>();
	}
	@Override
	public List<CartItem> getCartItemList(HttpServletRequest request,HttpServletResponse  response) {
		List<CartItem> itemList = getCartItemList(request);
		return itemList;
	}
	@Override
	public XizimallResult updateCartItemNum(Long itemId, Integer num, HttpServletRequest request,
			HttpServletResponse response) {
		List<CartItem> itemList = getCartItemList(request) ;
		//判断商品列表中是否存在此商品
		for(CartItem citem:itemList){
			if(citem.getId()==itemId){
				citem.setNum(num);
				break;
			}
		}
		//把购物车列表写入Cookies
		CookieUtils .setCookie(request, response, "TT_CART",JsonUtils.objectToJson(itemList) , true);
		return XizimallResult.ok();
	}
	@Override
	public XizimallResult deleteCartItem(Long itemId, HttpServletRequest request, HttpServletResponse response) {
		List<CartItem> itemList = getCartItemList(request) ;
		//判断商品列表中是否存在此商品
		for(CartItem citem:itemList){
			if(citem.getId()==itemId){
				itemList.remove(citem);
				break;
			}
		}
		//重新写入cookie
		CookieUtils.setCookie(request, response, "TT_CART", JsonUtils.objectToJson(itemList), true);
		return XizimallResult.ok();
	}

}
